/*
 * Copyright 2008 Daniel Freitas
 *
 * This file is part of Army Creator.
 * 
 * Army Creator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Army Creator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Army Creator.  If not, see <http://www.gnu.org/licenses/>.
 */
package cheesr.components;

import java.text.NumberFormat;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import cheesr.model.Cart;
import cheesr.model.Cheese;

/**
 * Panel for displaying the contents of a shopping cart. The cart shows the entries and the total value of the cart.
 * Each item can be removed by the user.
 */
public class ShoppingCartPanel extends Panel {
    private Cart cart;

    @SuppressWarnings("serial")
    public ShoppingCartPanel(String id, Cart cart) {
        super(id);
        this.cart = cart;
        WebMarkupContainer webMarkupContainer = new WebMarkupContainer("listOfItemsInCart");
        setOutputMarkupId(true);
        setOutputMarkupPlaceholderTag(true);
        add(webMarkupContainer);

        webMarkupContainer.add(new ListView("cart", new PropertyModel(this, "cart.cheeses")) {
            @Override
            protected void populateItem(ListItem item) {
                Cheese cheese = (Cheese) item.getModelObject();
                item.add(new Label("name", cheese.getName()));
                item.add(new Label("price", "$" + cheese.getPrice()));
                item.add(new AjaxFallbackLink("remove", new Model(cheese)) {
                    @Override
                    public void onClick(AjaxRequestTarget target) {
                        ShoppingCartPanel.this.cart.remove((Cheese) getModelObject());

                        if (target != null) {
                            target.addComponent(ShoppingCartPanel.this);
                        }
                    }
                });
            }
        });

        add(new Label("total", new Model() {
            @Override
            public Object getObject() {
                NumberFormat nf = NumberFormat.getCurrencyInstance();
                return nf.format(getCart().getTotal());
            }
        }));
    }

    private Cart getCart() {
        return cart;
    }

    private static final long serialVersionUID = -455191388583358994L;
}
